package com.mcmaster.android.data;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;

import com.google.android.maps.GeoPoint;

import android.content.Context;
import android.util.Log;

public class BuildingDatabase {
	// Members
	private static HashMap registry_; //! Stores buildings in memory
	private static int file_id_;
	private static Context context_;
	
	// Loads building data from text file
	public static void init(Context ctx, int resourceID) throws FileNotFoundException {
		file_id_ = resourceID;
		context_ = ctx;
		context_.getResources().openRawResource(file_id_); // Call once to check if file exists... 
	}
	public static List<Building> dumpBuildings() {
		InputStream read_in = context_.getResources().openRawResource(file_id_);
		Scanner scanner 	= new Scanner(read_in);
		String name		 	= "";
		String qr_code 		= "";
		String description 	= "";
		String image_id 	= "";
		List<String> hints 	= new ArrayList<String>();
		List<Building> result = new ArrayList<Building>();
		boolean found_qr	= false;
		String temp;
		GeoPoint point = null;
		
		
		try {
			while (scanner.hasNextLine()) {
				Scanner line = new Scanner(scanner.nextLine());
				
				line.useDelimiter(":");		
				if (line.hasNext()) {
					temp = line.next();
					line.findInLine(":(\\s)");
					if (temp.startsWith("#")) {
						line.nextLine();
					}
					else if (temp.equals("qr")) {
						qr_code = line.next();
					}
					if (temp.equals("name")) {
						name = line.next();
					}
					else if (temp.equals("location")) {
						 line.useDelimiter("\\d");
						 line.next();
						 line.useDelimiter(","); 
						 //javascript:void(prompt('',gApplication.getMap().getCenter()));
						 point = new GeoPoint((int)(line.nextDouble()*1E6),(int)(line.nextDouble()*1E6));
					}
					else if (temp.equals("description")) {
						description = line.next();
//						System.out.print(description + "\n"); 
					}
					else if (temp.equals("image")) {
						image_id = line.next();
//						System.out.print(image_id + "\n"); 
					}
					else if (temp.equals("hints")) {
						hints.add(line.next());
					}
					else if (temp.equals("end")) { 
						result.add(new Building(name, qr_code, point, description, image_id, hints));
						hints.clear();
					}
				}
			}
		}
		finally {
			scanner.close();
		}
		return result;
	}	
	
	// Returns the building that corresponds to QR code, if no hit, returns null
	public static Building getBuildingFromQR(String QR_code) {
		InputStream read_in = context_.getResources().openRawResource(file_id_);
		Scanner scanner 	= new Scanner(read_in);
		String name		 	= "";
		String qr_code 		= "";
		String description 	= "";
		String image_id 	= "";
		List<String> hints 	= new ArrayList<String>();
		boolean found_qr	= false;
		String temp;
		GeoPoint point = null;
		
		
		try {
			while (scanner.hasNextLine()) {
				Scanner line = new Scanner(scanner.nextLine());
				
				line.useDelimiter(":");		
				if (line.hasNext()) {
					temp = line.next();
					line.findInLine(":(\\s)");
					if (temp.startsWith("#")) {
						line.nextLine();
					}
					else if (temp.equals("qr")) {
						qr_code = line.next();
						if (qr_code.equals(QR_code)) { found_qr = true; } 
					}
					
					if (!found_qr) { continue; }
					
					if (temp.equals("name")) {
						name = line.next();
//						System.out.print(name + "\n"); 
					}
					else if (temp.equals("location")) {
						 line.useDelimiter("\\d");
						 line.next();
						 line.useDelimiter(","); 
						 //javascript:void(prompt('',gApplication.getMap().getCenter()));
						 point = new GeoPoint((int)(line.nextDouble()*1E6),(int)(line.nextDouble()*1E6));
					}
					else if (temp.equals("description")) {
						description = line.next();
//						System.out.print(description + "\n"); 
					}
					else if (temp.equals("image")) {
						image_id = line.next();
//						System.out.print(image_id + "\n"); 
					}
					else if (temp.equals("hints")) {
						hints.add(line.next());
						//System.out.print(hints. + "\n"); 
					}
					else if (temp.equals("end")) {
						System.out.print(name + qr_code + description+ image_id); 
						return new Building(name, qr_code, point, description, image_id, hints);
					}
				}
			}
		}
		finally {
			scanner.close();
		}
		return null;
	}
	
}